<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<link rel="stylesheet" type="text/css" href="${ctx}/css/tipswindown.css"/>
<script type="text/javascript" src="${ctx}/js/tipswindown.js"></script>

<h1 class="page-title">账号设置</h1>

<table class="display" id="example">
    <thead>
    <tr>
        <th>选择</th>
        <th>职务名称</th>
        <th>所属部门</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <s:if test="#request.rolePojos==null || #request.rolePojos.size()==0">
        <tr class="gradeX">
            <td colspan="4">暂无数据</td>
        </tr>
    </s:if>
    <s:iterator value="#request.rolePojos" var="rp" status="sta">
        <s:if test="#request.sta.odd">
            <tr class="gradeC">
        </s:if>
        <s:else>
            <tr class="gradeA">
        </s:else>
        <td><input type="checkbox" name="check_user" value="${r.id}"/></td>
        <td>${rp.duty.dutyName}</td>
        <td>${rp.duty.organization.path}</td>
        <td>
            <a href="javascript:void(0)" onclick="onShowDisplayDiv('${rp.role.resourceNames}')" class="button"><span class="icon" style="background:url(/images/img/zoom.png)">&nbsp;</span>查看</a>
            <a href="javascript:void(0)" onclick="onShowDiv('${rp.role.resourceIds}', '${rp.roleId}', ${rp.duty.id})" class="button"><span class="icon" style="background:url(/images/img/page_edit.png)">&nbsp;</span>设置</a>
        </td>
        </tr>
    </s:iterator>
    </tbody>
</table>
<p>
    <%@ include file="/WEB-INF/pages/common/main_orgtab_pager.jsp" %>
</p>

<!--control layer -->
<%--<div class="tabs side grid_12 leading">
    <div class="clear"></div>
    <div class="grid_12">
        <div class="form-action clearfix">
            <p>
                <input type="checkbox" id="form-all" value="1" onchange="checkAll(this,'check_user')"/><label
                    for="form-all">全选</label>

                <button class="button" type="button" onclick="deleteUsers()">批量删除</button>
                <a class="button" href="javascript:updateSelectedRoles()"><span style="background:url(${ctx}/images/img/page_edit.png)" class="icon">&nbsp;</span>修改</a>
                <a class="button" href="#authority/role_input.xhtml?organizationId=${organizationId}"> <span class="icon" style="background:url(${ctx}/images/img/add.png)">&nbsp;</span>添加</a>
            </p>
        </div>
    </div>
</div>--%>
<!--control layer end-->

<input type="hidden" id="current_role_id">
<input type="hidden" id="current_duty_id">
<input type="hidden" id="current_resource_ids">
<div style="display: none;" id="resources_list">
    <div style="height: 500px;overflow:auto">
    <input type="checkbox" name="all_checkbox" style="margin-left: 10px;" onclick="checkAll(this)"> 全选
    <table class="display">
        <tbody>
        <s:if test="#request.resources==null || #request.resources.size()==0">
            <tr>
                <td colspan="5">暂无数据</td>
            </tr>
        </s:if>
        <s:iterator value="#request.resources" var="r" status="sta">
            <tr></tr>
            <td><input type="checkbox" value="${r.id}" id="resource_${r.id}" name="resource_checkbox"/><label style="margin-right: 10px;" for="resource_${r.id}">&nbsp;${r.name}</label></td>
            </tr>
        </s:iterator>
        </tbody>
        <tr>
            <td></td>
        </tr>
    </table>
    </div>
    <p align="center" style="height: 50px;">
        <button onclick="selectRoles()" type="button" class="button">确定</button>
        <button onclick="cancelRoles()" type="button" class="button">取消</button>
    </p>
</div>

<div style="display: none;" id="resources_display">
    <div style="height: 300px;overflow:auto">
    </div>
</div>


<script type="text/javascript">
    function onShowDisplayDiv(resourceNames){
        tipsWindown("权限浏览","id:resources_display","500","300","true","","true","resources_display");
        var tempHtmlStr = "";
        if(resourceNames != null && resourceNames.length > 0){
            var temps = resourceNames.split(",");
            for(var i=0; i<temps.length; i++){
                tempHtmlStr += '<span style="margin: 2px 4px;">'+temps[i]+'</span>';
            }
        }else{
            tempHtmlStr += '<span style="margin: 2px 4px;">没有设置权限</span>';
        }
        $("#windown-content").find("div").html(tempHtmlStr);
    }

    function onShowDiv(resourceIds, roleId, dutyId){
        //alert(roleId+":"+dutyId)
        $("#current_role_id").val(roleId);
        $("#current_duty_id").val(dutyId);
        $("#current_resource_ids").val(resourceIds);
        //tipsWindown("选择员工","url:post?${ctx}/employee/staff_list.xhtml?operation=select&organizationId=${organizationId}","900","350","true","","true","text");
        tipsWindown("权限设置","id:resources_list","500","600","true","","true","resources_list");
        if(roleId != "" && parseInt(roleId) > 0){
            if(resourceIds != null && resourceIds.length > 2){
                $("#windown-content").find("input[type='checkbox'][name='resource_checkbox']").each(function(){
                    if(resourceIds.indexOf(","+this.value+",") > -1){
                        $(this).attr("checked", true);
                    }else{
                        $(this).attr("checked", false);
                    }
                });
            }
        }
    }

    function selectRoles(){
        var resources = new Array();
        $("#windown-content").find("input[type='checkbox'][name='resource_checkbox']:checked").each(function(){
            resources.push(this.value);
        });
        var old_resource_ids = $("#current_resource_ids").val();
        var new_resource_ids = resources.join(",");
        //没有改变权限时直接返回
        if(old_resource_ids == new_resource_ids || old_resource_ids == ","+new_resource_ids+","){
            cancelRoles();
            return;
        }

        $.ajax({
            type: 'POST',
            url:"${ctx}/authority/role-save.xhtml",
            data:{"current_role_id":$("#current_role_id").val(), "current_duty_id":$("#current_duty_id").val(), "resource_ids":new_resource_ids},
            dataType:"json",
            success: function(result){
                if(result == "success"){
                    alert("权限设置成功");
                    location.reload();
                }else{
                    alert("权限设置失败")
                }
                cancelRoles();
            }
        });
    }
    function cancelRoles(){
        closeWindown();
    }

    function checkAll(obj){
        if($(obj).attr("checked")){
            $("#windown-content").find("input[type='checkbox'][name='resource_checkbox']").attr("checked", true)
        }else{
            $("#windown-content").find("input[type='checkbox'][name='resource_checkbox']").attr("checked", false)
        }
    }
</script>